#include <cstdio>
#include <cstring>
#include <cctype>
#include <queue>
using namespace std;

const int maxs = 1005;
const int maxslen = 10;
const int maxp = 15;
const int maxv = 30;
int n, t[5], quantum, running[maxp], var[maxv];
char program[maxs][maxslen];
bool locked;
deque<int> ready;
queue<int> block;

void run(int);

int main() {
  int k;
  scanf("%d", &k);
  while (k--) {
    scanf("%d %d %d %d %d %d %d\n", &n, &t[0], &t[1], &t[2], &t[3], &t[4], &quantum);
    memset(var, 0, sizeof(var));

    int line = 0, i;
    for (i = 0; i < n; i++) {
      fgets(program[line++], maxslen, stdin);
      running[i] = line - 1;
      while (!(program[line - 1][2] == 'd')) {
        fgets(program[line++], maxslen, stdin);
      }
      ready.push_back(i);
    }

    locked = false;
    while (!ready.empty()) {
      int pid = ready.front();
      ready.pop_front();
      run(pid);
    }

    if (k) {
      printf("\n");
    }
  }
  return 0;
}

void run(int pid) {
  int q = quantum;
  while (q > 0) {
    char *statement = program[running[pid]];
    switch (statement[2]) {
      case '=':
        var[statement[0] - 'a'] = isdigit(statement[5]) ? ((statement[4] - '0') * 10) + (statement[5] - '0') : statement[4] - '0';
        q -= t[0];
        break;
      case 'i': /* print */
        printf("%d: %d\n", pid + 1, var[statement[6] - 'a']);
        q -= t[1];
        break;
      case 'c': /* lock */
        if (locked) {
          block.push(pid);
          return;
        }
        locked = true;
        q -= t[2];
        break;
      case 'l': /* unlock */
        locked = false;
        if (!block.empty()) {
          int next_pid = block.front();
          block.pop();
          ready.push_front(next_pid);
        }
        q -= t[3];
        break;
      case 'd': /* end */
        return;
    }
    running[pid]++;
  }
  ready.push_back(pid);
}
